<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 *  数组转换成树形数组
 *  $list s数据源
 *  $pid 父级 key
 *  $p_value 父级 顶级默认值
 *  $child_key 子级key
 *  $level 层级
 */
function array_to_tree($list, $level = 5, $pid = "p_id", $p_value = 0, $primary_key = "id")
{
    $return_array = [];
    foreach ($list as $tmp) {
        if ($tmp[$pid] == $p_value) {
            $return_array[] = $tmp;
        }
    }
    if (!empty($return_array)) {
        foreach ($return_array as $key => $p_tmp) {
            build_tree($return_array[$key], $list, $level, $pid, $primary_key);
        }
    }
    return $return_array;
}
/**
 *  递归循环数组 构成树形数组
 *
 *  $build_array  起始数组
 *  $list  数据集
 *  $pid 父级 key
 *  $p_value 父级 顶级默认值
 *  $child_key 子级key
 *  $level 层级
 */
function build_tree(&$build_array, $list, $level = 5, $pid = "p_id", $primary_key = "id")
{
    foreach ($list as $tmp1) {
        if ($tmp1["level"] <= $level) {
            if ($build_array[$primary_key] == $tmp1[$pid]) {
                build_tree($tmp1, $list, $level, $pid, $primary_key);

                $build_array["_child"][] = $tmp1;
            }
        }
    }
}

/**
 *  递归循环数组 选择框 层级效果
 *
 *  $build_array  起始数组
 *  $list  数据集
 *  $pid 父级 key
 *  $p_value 父级 顶级默认值
 *  $child_key 子级key
 *  $level 层级
 */
function build_select_tree(&$build_array, $list, $level = 5, $pid = "p_id", $primary_key = "id", &$return_array = [])
{
    foreach ($list as $tmp1) {
        if ($tmp1["level"] <= $level) {
            if ($build_array[$primary_key] == $tmp1[$pid]) {
                $tmp1["title"] = str_prefix($tmp1["title"], $tmp1["level"], " — ");
                $return_array[] = $tmp1;
                build_select_tree($tmp1, $list, $level, $pid, $primary_key, $return_array);

            }
        }
    }
}

/**
 *  选择框 层级效果
 */
function array_to_select($list, $level = 5, $pid = "p_id", $p_value = 0, $primary_key = "id")
{
    $return_array = [];
    foreach ($list as $tmp) {
        if ($tmp[$pid] == $p_value) {
            $return_array[] = $tmp;
        }
    }
    $return_array_tmp = [];

    if (!empty($return_array)) {
        foreach ($return_array as $key => $p_tmp) {
            $p_tmp["title"] = str_prefix($p_tmp["title"], $p_tmp["level"], "—");
            $return_array_tmp[] = $p_tmp;
            build_select_tree($return_array[$key], $list, $level, $pid, $primary_key, $return_array_tmp);
        }
    }
    return $return_array_tmp;
}

/**
 *  前面追加字符
 * str_prefix('test',3,'-')=>returns'---test'
 */

function str_prefix($str, $n = 1, $char = " ")
{
    for ($x = 0; $x < $n; $x++) {$str = $char . $str;}

    return $str;
}

/**
 *  后面边追加字符
 * str_suffix('test',3,'-')=> returns 'test---'
 */
function str_suffix($str, $n = 1, $char = " ")
{
    for ($x = 0; $x < $n; $x++) {$str = $str . $char;}
    return $str;
}

/**
 * 加密
 */
function HashEncrypt($str)
{
    $t_hasher = new \PasswordHash(8, false);
    $hash = $t_hasher->HashPassword($str);
    return $hash;
}

/**
 * 验证加密
 */
function HashVerifyEncrypt($str, $hash)
{
    $t_hasher = new \PasswordHash(8, false);
    $check = $t_hasher->CheckPassword($str, $hash);
    return $check;
}

/**
 * 第几周
 */
function monthWeek()
{
   
    $week = date('W',time());

    return $week;
}

/**
 *  获取用户菜单
 */
function get_user_menu($type)
{
    $user_menu = session("user_menu");
    $menuData = [];

    $user_menu = unserialize($user_menu);

    if (empty($user_menu)) {
        return $menuData;
    }
    foreach ($user_menu as $menu) {
        if ($menu["type"] == $type) {
            $menuData[] = $menu;
        }
    }
    return $menuData;
}

/**
 *  检查是否有权限
 */
function check_access($url)
{
    $user_menu = session("user_menu");
    $url = strtolower($url);
    $user_menu = unserialize($user_menu);
    $data = false;
    if (session("role_id") == 1) {
        $data = true;
    } else {
        foreach ($user_menu as $menu) {
            if ($menu["url"] == $url) {
                $data = true;
                break;
            }
        }
    }
    return $data;
}
